package com.meta.nd.util;

import com.meta.nd.vo.Fd;
import com.meta.nd.vo.FdCompareResult;
import com.meta.nd.vo.Nd;
import com.meta.nd.vo.NdCompareResult;

import java.util.ArrayList;
import java.util.List;

public class NdUtil {

    public static NdCompareResult compareNd(Nd refNd, Nd nd){
        NdCompareResult result = new NdCompareResult(nd.getNdCode(),nd.getFinalName());
        if ( refNd.getFinalName().equals(nd.getFinalName())){
            result.setOperation(NdCompareResult.NO_CHANGE);
           result.getFdCompareResults().addAll( compareFd(refNd,nd) );
        }
        else{
            result.setOperation(NdCompareResult.ADD);
        }

        for(Nd refChild : refNd.getChildren()){
            boolean isFound = false;
            for( Nd child : nd.getChildren()){
                if ( refChild.getFinalName().equals(child.getFinalName())){
                    isFound = true;
                    result.getChildren().add(compareNd(refChild,child));
                    break;
                }
            }
            if ( !isFound ){
                NdCompareResult childResult = new NdCompareResult(refChild.getNdCode(),refChild.getFinalName());
                childResult.setOperation(NdCompareResult.REMOVE);
                result.getChildren().add(childResult);
            }
        }

        for( Nd child:nd.getChildren()){
            boolean isFound = false;
            for( Nd refChild : refNd.getChildren()){
                if ( refChild.getFinalName().equals(child.getFinalName())){
                    isFound = true;
                    result.getChildren().add(compareNd(refChild,child));
                    break;
                }
            }
            if ( !isFound ){
                NdCompareResult childResult = new NdCompareResult(child.getNdCode(),child.getFinalName());
                childResult.setOperation(NdCompareResult.ADD);
                result.getChildren().add(childResult);
            }
        }
        return result;
    }

    private static List<FdCompareResult> compareFd(Nd refNd, Nd nd){
        List<FdCompareResult>  results = new ArrayList<>();
        for(Fd fd : nd.getFds()){
            boolean found = false;
            for( Fd refFd : refNd.getFds()){
                if (refFd.getFdName() == null)
                    throw new RuntimeException("Nd上的属性名为空!");
                if( refFd.getFdName().equals(fd.getFdName())){
                    found = true;
                    FdCompareResult result = new FdCompareResult(nd.getNdCode(),fd.getFdName());
                    result.setOperation(NdCompareResult.NO_CHANGE);
                    results.add(result);
                    break;
                }
            }
            if ( !found ){
                FdCompareResult result = new FdCompareResult(nd.getNdCode(),fd.getFdName());
                result.setOperation(NdCompareResult.ADD);
                results.add(result);
            }
        }
        for(Fd refFd : refNd.getFds()){
            boolean found = false;
            for( Fd fd : nd.getFds()){
                if (fd.getFdName() == null)
                    throw new RuntimeException("Nd上的属性名为空!");
                if( fd.getFdName().equals(refFd.getFdName())){
                    found = true;
                    break;
                }
            }
            if ( !found ){
                FdCompareResult result = new FdCompareResult(refNd.getNdCode(),refFd.getFdName());
                result.setOperation(NdCompareResult.REMOVE);
                results.add(result);
            }
        }
        return results;
    }

}
